package com.jichangxiu.generator.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.jichangxiu.common.constant.Constants;
import com.jichangxiu.common.utils.StrUtils;
import com.jichangxiu.generator.constant.GeneratorConstants;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

public class GeneratorUtils {

    // 获取包前缀
    public static String getPackagePrefix(String packageName) {
        int lastIndex = packageName.lastIndexOf(".");
        return StringUtils.substring(packageName, 0, lastIndex);
    }

    // 表名转换成 Java 类名
    public static String convertClassName(String tableName) {
        Boolean autoRemovePre = Constants.JCX_PROPERTIES.getGeneratorProperties().getAutoRemovePre();
        String tablePrefix = Constants.JCX_PROPERTIES.getGeneratorProperties().getTablePrefix();
        if (autoRemovePre && StrUtil.isNotEmpty(tablePrefix)) {
            String[] searchList = tablePrefix.split(",");
            tableName = replaceFirst(tableName, searchList);
        }
        return StrUtils.convertToCamelCase(tableName);
    }

    // 批量替换前缀
    public static String replaceFirst(String replace, String[] searchList) {
        String text = replace;
        for (String searchString : searchList) {
            if (replace.startsWith(searchString)) {
                text = replace.replaceFirst(searchString, "");
                break;
            }
        }
        return text;
    }

    // 获取模块名
    public static String getModuleName(String packageName) {
        int lastIndex = packageName.lastIndexOf(".");
        int nameLength = packageName.length();
        return StringUtils.substring(packageName, lastIndex + 1, nameLength);
    }

    // 获取业务名
    public static String getBusinessName(String tableName) {
        int lastIndex = tableName.lastIndexOf("_");
        int nameLength = tableName.length();
        return StringUtils.substring(tableName, lastIndex + 1, nameLength);
    }

    // 获取数据库类型字段
    public static String getDbType(String columnType) {
        if (StringUtils.indexOf(columnType, "(") > 0) {
            return StringUtils.substringBefore(columnType, "(");
        } else {
            return columnType;
        }
    }

    // 校验数组是否包含指定值
    public static boolean arraysContains(String[] arr, String targetValue) {
        return Arrays.asList(arr).contains(targetValue);
    }

    // 获取字段长度
    public static Integer getColumnLength(String columnType) {
        if (StringUtils.indexOf(columnType, "(") > 0) {
            String length = StringUtils.substringBetween(columnType, "(", ")");
            return Integer.valueOf(length);
        } else {
            return 0;
        }
    }

    // 获取 Java 类型
    public static String getJavaType(String columnType) {
        String dataType = getDbType(columnType);
        if (arraysContains(GeneratorConstants.COLUMN_TYPE_STR, dataType) || arraysContains(GeneratorConstants.COLUMN_TYPE_TEXT, dataType)) {
            return GeneratorConstants.TYPE_STRING;
        } else if (arraysContains(GeneratorConstants.COLUMN_TYPE_TIME, dataType)) {
            return GeneratorConstants.TYPE_DATE;
        } else if (arraysContains(GeneratorConstants.COLUMN_TYPE_NUMBER, dataType)) {
            // 如果是浮点型 统一用 BigDecimal
            String[] str = StringUtils.split(StringUtils.substringBetween(columnType, "(", ")"), ",");
            if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0) {
                return GeneratorConstants.TYPE_BIG_DECIMAL;
            }
            // 如果是整形
            else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10) {
                return GeneratorConstants.TYPE_INTEGER;
            }
            // 长整形
            else {
                return GeneratorConstants.TYPE_LONG;
            }
        }
        return GeneratorConstants.TYPE_STRING;
    }

    // 获取 查询类型
    public static String getQueryType(String columnName) {
        if (StringUtils.endsWithIgnoreCase(columnName, "code")
                || StringUtils.endsWithIgnoreCase(columnName, "name")
                || StringUtils.endsWithIgnoreCase(columnName, "remark")) {
            return GeneratorConstants.QUERY_LIKE;
        }
        return GeneratorConstants.QUERY_EQ;
    }

    // 获取控件类型
    public static String getHtmlType(String columnType, String columnName) {
        String dataType = getDbType(columnType);
        if (arraysContains(GeneratorConstants.COLUMN_TYPE_STR, dataType) || arraysContains(GeneratorConstants.COLUMN_TYPE_TEXT, dataType)) {
            // 字符串长度超过500设置为文本域
            Integer columnLength = getColumnLength(columnType);
            return columnLength >= 500 || arraysContains(GeneratorConstants.COLUMN_TYPE_TEXT, dataType) ? GeneratorConstants.HTML_TEXTAREA : GeneratorConstants.HTML_INPUT;
        } else if (arraysContains(GeneratorConstants.COLUMN_TYPE_TIME, dataType)) {
            return GeneratorConstants.HTML_DATETIME;
        } else if (arraysContains(GeneratorConstants.COLUMN_TYPE_NUMBER, dataType)) {
            return GeneratorConstants.HTML_INPUT;
        } else {
            // 状态字段设置单选框
            if (StringUtils.endsWithIgnoreCase(columnName, "status") || StringUtils.endsWithIgnoreCase(columnName, "enabled")) {
                return GeneratorConstants.HTML_SWITCH;
            }
            // 类型&性别字段设置下拉框
            else if (StringUtils.endsWithIgnoreCase(columnName, "type") || StringUtils.endsWithIgnoreCase(columnName, "sex")) {
                return GeneratorConstants.HTML_SELECT;
            }
            // 图片字段设置图片上传控件
            else if (StringUtils.endsWithIgnoreCase(columnName, "image")) {
                return GeneratorConstants.HTML_IMAGE_UPLOAD;
            }
            // 文件字段设置文件上传控件
            else if (StringUtils.endsWithIgnoreCase(columnName, "file")) {
                return GeneratorConstants.HTML_FILE_UPLOAD;
            }
            // 内容字段设置富文本控件
            else if (StringUtils.endsWithIgnoreCase(columnName, "content")) {
                return GeneratorConstants.HTML_EDITOR;
            }
        }
        return GeneratorConstants.HTML_INPUT;
    }

    // 获取权限前缀
    public static String getAuthPrefix(String moduleName, String businessName) {
        return StrUtils.format("{}:{}", moduleName, businessName);
    }

    public static String getParentMenuId(JSONObject paramsObj) {
        if (ObjectUtil.isNotEmpty(paramsObj) && paramsObj.containsKey(GeneratorConstants.PARENT_MENU_ID)
                && StringUtils.isNotEmpty(paramsObj.getString(GeneratorConstants.PARENT_MENU_ID))) {
            return paramsObj.getString(GeneratorConstants.PARENT_MENU_ID);
        }
        return Constants.JCX_PROPERTIES.getGeneratorProperties().getDefaultMenuId();
    }

}
